package edu.pku.sei.sct.util;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import edu.pku.sei.AMT.Section;

public class SectionSet{
	static final private Comparator<Section> comparator =new Comparator<Section>() {
		@Override
		public int compare(Section arg0, Section arg1) {
			if(arg0==arg1)
				return 0;
			return arg0.hashCode()-arg1.hashCode();
		}
	};
	
	private Section[] array;
	
	public Section[]  getArray() {
		return array;
	}


	public SectionSet(List<Section> lists) {
		array = new Section[lists.size()];
		lists.toArray(array);
		Arrays.sort(array, comparator);
	}
	
	public boolean equals(Object r) {
		if(this == r) return true;
		if(r instanceof SectionSet) {
			SectionSet rs = (SectionSet)r;
			return Arrays.equals(array, rs.array);
		} else 
			return false;
	}
	
	private Integer hashCode = null;
	
	public int hashCode() {
		if(hashCode==null) {
			int i = 0;
			for(Section s : array) {
				i+=s.hashCode();
			}
			hashCode = i;
		}
		return hashCode;
	}
}
